const componentOptions = {
  // 组件选项
  options: {
    multipleSlots: true,
  },
  behaviors: [],
  properties: {},
  // 组件数据
  data: {
    helmet: null,
    miku: null,
    loaded: false,
    camera: null,
    tmpV3: {},
    scene: {},
    isPageHidden: false, // 页面是否处于隐藏状态
  },
  // 数据监听器
  observers: {},
  // 组件方法
  methods: {
    init: function () {
    },
    handleReady: function ({ detail }) {
      this.scene = detail.value;
      const xrFrameSystem = wx.getXrFrameSystem();
     
    },

    handleTick: function () {
      if (!this.tracker) return;
      const { gesture, score } = this.tracker;
      if (score < 0.5 || gesture === this.gesture) {
        return;
      }
      this.gesture = gesture;
      gesture===6&&wx.showToast({
        title: '好！',
      });
      gesture===14&&wx.showToast({
        title: '唉....',
      });
    },
    handleTrackerSwitch:function({detail}){
      const active=detail.value;
      const video=this.scene.assets.getAsset('video-texture','hikari');
      active?video.play():video.stop();
    },
    handleAssetsLoaded: function ({ detail }) {
      this.setData({
        loaded: true
      });
      const el = this.scene.getElementById('tracker');
      this.tracker = el.getComponent(wx.getXrFrameSystem().ARTracker);
      this.gesture = -1;
    },

    handleAssetsProgress: function ({ detail }) {
      console.log('assets progress', detail.value);
    },
    handleTouchModel: function ({ detail }) {
      const { target } = detail.value;
      console.log(`模型： ${target.id}`);
      this[target.id].color = `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.5)`;

    },

    getScreenPosition: function (value) {
      const { el, color } = value;
      const xrFrameSystem = wx.getXrFrameSystem();
      this.tmpV3.set(el.getComponent(xrFrameSystem.Transform).worldPosition);
      const clipPos = this.camera.convertWorldPositionToClip(this.tmpV3);
      const { frameWidth, frameHeight } = this.scene;
      return [((clipPos.x + 1) / 2) * frameWidth, (1 - (clipPos.y + 1) / 2) * frameHeight, color, el.id];
    }

  },
  // 组件生命周期
  lifetimes: {
    created() { },
    attached() {
      this.init()
    },
    ready() { },
    moved() { },
    detached() { },
  },
  definitionFilter() { },
  // 页面生命周期
  pageLifetimes: {
    // 页面被展示
    show() {
      const { isPageHidden } = this.data

      // show事件发生前，页面不是处于隐藏状态时
      if (!isPageHidden) {
        return
      }

      // 重新执行定时器等操作
    },
    // 页面被隐藏
    hide() {
      this.setData({
        isPageHidden: true,
      })

      // 清除定时器等操作
    },
    // 页面尺寸变化时
    resize() { },
  },
}

Component(componentOptions)
